package com.viphuli.app.tool.fragment;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.Bind;
import butterknife.OnClick;

import com.loopj.android.http.RequestParams;
import com.offroader.core.DoubleClickExitHelper;
import com.umeng.update.UmengUpdateAgent;
import com.umeng.update.UpdateStatus;
import com.viphuli.app.tool.R;
import com.viphuli.app.tool.bean.part.AccountUser;
import com.viphuli.app.tool.bean.part.ArrangeInfo;
import com.viphuli.app.tool.bean.part.ArrangeRecord;
import com.viphuli.app.tool.common.AccessTokenKeeper;
import com.viphuli.app.tool.handler.MyBaseHttpResponseHandler.HandlerType;
import com.viphuli.app.tool.http.ApiRequest;
import com.viphuli.app.tool.utils.ImageUtils;

public class HomeFragment extends BaseProgressFragment {

	private DoubleClickExitHelper mDoubleClickExit;

	@Bind(R.id.id_home_arrange_record)
	protected View recordView;

	@Bind(R.id.id_home_arrange_record_empty)
	protected View recordEmptyView;

	@Bind(R.id.id_home_arrange)
	protected View arrangeView;

	@Bind(R.id.id_home_arrange_action)
	protected TextView arrangeAction;

	@Bind(R.id.id_home_arrange_action_layout)
	protected View arrangeActionLayout;

	@Bind(R.id.id_home_arrange_layout)
	protected TextView arrangeLayout;

	@Bind({ R.id.id_home_my_arrange_one, R.id.id_home_my_arrange_two })
	protected List<View> myArranges;

	@Bind(R.id.id_home_arrange_empty)
	protected View arrangeEmptyView;

	@Bind(R.id.id_home_user)
	protected View userView;

	@Bind(R.id.id_home_user_empty)
	protected View userEmptyView;

	@Bind(R.id.id_circle_member_girdview)
	protected GridView memberGirdView;
	
	@Bind(R.id.id_home_arrange_btn_layout)
	protected View arrangeBtnLayout;

	private List<ArrangeRecord> arrangeRecordInfoList;

	private List<AccountUser> userList = new ArrayList<AccountUser>();

	private List<ArrangeInfo> myArrangeList;
	
	private Menu menu;

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		//友盟更新
		UmengUpdateAgent.setUpdateUIStyle(UpdateStatus.STYLE_DIALOG);
		UmengUpdateAgent.silentUpdate(getActivity());

	}

	@Override
	protected int obtainContentRes() {
		return R.layout.frag_home;
	}

	@Override
	protected void initParams() {
	}

	@Override
	protected void initView() {

		//设置用户头像
		ImageView leftImg = caller.getLeftImg();
		leftImg.setVisibility(View.VISIBLE);

		//排班记录
		recordView.setVisibility(View.GONE);
		recordEmptyView.setVisibility(View.VISIBLE);

		//我的排班 
		arrangeView.setVisibility(View.GONE);
		arrangeEmptyView.setVisibility(View.VISIBLE);

		//小组成员
		userView.setVisibility(View.GONE);
		userEmptyView.setVisibility(View.VISIBLE);

		mDoubleClickExit = new DoubleClickExitHelper(getActivity());
		caller.getLeftTitle().setVisibility(View.GONE);

		AccessTokenKeeper tokenKeeper = AccessTokenKeeper.readAccessToken(getActivity());
		if (tokenKeeper.isLogin()) {

			ImageUtils.load(caller.getLeftImg(), tokenKeeper.getUser().getPortrait());

		} else {

			caller.getLeftImg().setImageResource(R.drawable.ic_def_portrait);

		}
		leftImg.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View view) {
				//进入用户中心
				AccessTokenKeeper tokenKeeper = AccessTokenKeeper.readAccessToken(getActivity());

				if (tokenKeeper.isLogin())
					AccountFragment.go(getActivity(), tokenKeeper.getOpenId());
				else
					AccountLoginFragment.go(getActivity());

			}
		});

	}

	@Override
	public void onStart() {
		super.onStart();

//		AccessTokenKeeper tokenKeeper = AccessTokenKeeper.readAccessToken(getActivity());
//		if (tokenKeeper.isLogin()) {
//
//			AccountUser user = tokenKeeper.getUser();
//
//			//根据用户身份控制界面展示 
//			if (UserType.arrange_admin.getValue() == user.getUserType()) {
//
////				arrangeActionLayout.setVisibility(View.VISIBLE);
//				arrangeBtnLayout.setVisibility(View.VISIBLE);
//
//				//设置左图标
//				Drawable drawableLeft = getResources().getDrawable(R.drawable.ic_home_item_arrange_recent);
//				/// 这一步必须要做,否则不会显示.  
//				drawableLeft.setBounds(0, 0, drawableLeft.getMinimumWidth(), drawableLeft.getMinimumHeight());
//
//				arrangeLayout.setCompoundDrawables(drawableLeft, null, null, null);
//
//			} else {
//
////				arrangeActionLayout.setVisibility(View.GONE);
//				arrangeActionLayout.setVisibility(View.GONE);
//
//				//设置右箭头
//				Drawable drawableRight = getResources().getDrawable(R.drawable.sl_btn_right_arrow);
//				/// 这一步必须要做,否则不会显示.  
//				drawableRight.setBounds(0, 0, drawableRight.getMinimumWidth(), drawableRight.getMinimumHeight());
//
//				//设置左图标
//				Drawable drawableLeft = getResources().getDrawable(R.drawable.ic_home_item_arrange_recent);
//				/// 这一步必须要做,否则不会显示.  
//				drawableLeft.setBounds(0, 0, drawableLeft.getMinimumWidth(), drawableLeft.getMinimumHeight());
//
//				arrangeLayout.setCompoundDrawables(drawableLeft, null, drawableRight, null);
//
//			}
//
//		}

		initRequest(HandlerType.loading);
	}

	@Override
	protected void initData() {

	}

	@Override
	protected void initRequest(HandlerType handlerType) {

		AccessTokenKeeper tokenKeeper = AccessTokenKeeper.readAccessToken(getActivity());

		if (tokenKeeper.isLogin()) {

			RequestParams params = new RequestParams();
			params.put("uid", tokenKeeper.getOpenId());
			params.put("circle_id", tokenKeeper.getUser().circleId());

			ApiRequest.home.request(this, params, handlerType);

		} else {

			if (HandlerType.refresh.equals(handlerType)) {

				//隐藏下拉刷新
				setRefreshing(false);
			}

			recordEmptyView.setVisibility(View.VISIBLE);
			recordView.setVisibility(View.GONE);

			arrangeEmptyView.setVisibility(View.VISIBLE);
			arrangeView.setVisibility(View.GONE);

			userEmptyView.setVisibility(View.VISIBLE);
			userView.setVisibility(View.GONE);

		}

	}

	@Override
	public void onBackPressed() {

		// 是否退出应用
		mDoubleClickExit.onBackPressed();

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		this.menu = menu;
		
		getActivity().getMenuInflater().inflate(R.menu.menu_home_right, menu);
		
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		if (R.id.id_menu_home_right_member_holiday == item.getItemId()) {

			HolidayFragment.go(getActivity());

			return true;

		} else if (R.id.id_menu_home_right_member_add == item.getItemId()) {

			CircleUserAddFragment.go(getActivity());

			return true;

		}

		return false;
	}

	@OnClick(R.id.id_menu_home_right_member_holiday)
	public void goToHoliday() {
		HolidayFragment.go(getActivity());
	}
	
	@OnClick(R.id.id_home_arrange_btn)
	public void goToArray(){
		
		AccessTokenKeeper tokenKeeper = AccessTokenKeeper.readAccessToken(getActivity());

		if (!tokenKeeper.isLogin()) {

			AccountLoginFragment.go(getActivity());

		} else if (tokenKeeper.getUser().circleId() == 0) {

			CircleCreateFragment.go(getActivity());

		} else {

			Bundle args = new Bundle();
			args.putInt(ArrangeViewFragment.ACTION_TYPE, ArrangeViewFragment.ACTION_ARRANGE);

			ArrangeViewFragment.go(getActivity(), args);

		}
		
	}

	@OnClick(R.id.id_home_my_arrange_one)
	public void clickArrangeOne() {

		if (myArrangeList != null && !myArrangeList.isEmpty()) {

			ArrangeInfo arrangeInfo = myArrangeList.get(0);

			Bundle args = new Bundle();
			args.putInt(ArrangeViewFragment.ACTION_TYPE, ArrangeViewFragment.ACTION_EXCHANGE);
			args.putString("def_select_uid", arrangeInfo.getToUser().getUid());
			args.putLong("def_select_time", arrangeInfo.getArrangeTypeinfo().start());

			ArrangeViewFragment.go(getActivity(), args);

		}

	}

	@OnClick(R.id.id_home_my_arrange_two)
	public void clickArrangeTwo() {

		if (myArrangeList != null && !myArrangeList.isEmpty()) {

			ArrangeInfo arrangeInfo = myArrangeList.get(1);

			Bundle args = new Bundle();
			args.putInt(ArrangeViewFragment.ACTION_TYPE, ArrangeViewFragment.ACTION_EXCHANGE);
			args.putString("def_select_uid", arrangeInfo.getToUser().getUid());
			args.putLong("def_select_time", arrangeInfo.getArrangeTypeinfo().start());
			ArrangeViewFragment.go(getActivity(), args);

		}

	}

	@OnClick({ R.id.id_home_arrange_record_empty, R.id.id_home_arrange_empty, R.id.id_home_user_empty })
	public void clickEmptyView() {
		AccessTokenKeeper tokenKeeper = AccessTokenKeeper.readAccessToken(getActivity());

		if (!tokenKeeper.isLogin()) {
			AccountLoginFragment.go(getActivity());
		} else if (tokenKeeper.getUser().circleId() == 0) {
			CircleCreateFragment.go(getActivity());
		}

	}

	@OnClick(R.id.id_home_arrange_record_layout)
	public void goToArrangeRecordList() {

		ArrangeRecordListFragment.go(getActivity());

	}

	@OnClick({ R.id.id_home_arrange_layout })
	public void goToArrangeChange() {

		AccessTokenKeeper tokenKeeper = AccessTokenKeeper.readAccessToken(getActivity());

		if (!tokenKeeper.isLogin()) {

			AccountLoginFragment.go(getActivity());

		} else if (tokenKeeper.getUser().circleId() == 0) {

			CircleCreateFragment.go(getActivity());

		} else {

			Bundle args = new Bundle();
			args.putInt(ArrangeViewFragment.ACTION_TYPE, ArrangeViewFragment.ACTION_EXCHANGE);
			ArrangeViewFragment.go(getActivity(), args);

		}

	}
	
	@OnClick({ R.id.id_home_arrange_action })
	public void goToArrange() {

		AccessTokenKeeper tokenKeeper = AccessTokenKeeper.readAccessToken(getActivity());

		if (!tokenKeeper.isLogin()) {

			AccountLoginFragment.go(getActivity());

		} else if (tokenKeeper.getUser().circleId() == 0) {

			CircleCreateFragment.go(getActivity());

		} else {

			Bundle args = new Bundle();
			args.putInt(ArrangeViewFragment.ACTION_TYPE, ArrangeViewFragment.ACTION_ARRANGE);
			ArrangeViewFragment.go(getActivity(), args);

		}

	}

	@OnClick(R.id.id_home_circle_member_layout)
	public void goToMemberList() {

		//		CircleMemberFragment.go(getActivity());

		AccessTokenKeeper tokenKeeper = AccessTokenKeeper.readAccessToken(getActivity());

		if (tokenKeeper.isLogin()) {

			if (tokenKeeper.getUser().circleId() == 0)
				CircleCreateFragment.go(getActivity());
			else
				CircleInfoFragment.go(getActivity());

		} else
			AccountLoginFragment.go(getActivity());

	}

	public View getRecordView() {
		return recordView;
	}

	public View getRecordEmptyView() {
		return recordEmptyView;
	}

	public View getArrangeView() {
		return arrangeView;
	}

	public View getArrangeEmptyView() {
		return arrangeEmptyView;
	}

	public View getUserView() {
		return userView;
	}

	public View getUserEmptyView() {
		return userEmptyView;
	}

	public List<View> getMyArranges() {
		return myArranges;
	}

	public GridView getMemberGirdView() {
		return memberGirdView;
	}

	public List<AccountUser> getUserList() {
		return userList;
	}

	public View getArrangeAction() {
		return arrangeAction;
	}

	public void setArrangeRecordInfoList(List<ArrangeRecord> arrangeRecordInfoList) {
		this.arrangeRecordInfoList = arrangeRecordInfoList;
	}

	public List<ArrangeRecord> getArrangeRecordInfoList() {
		return arrangeRecordInfoList;
	}

	public void setMyArrangeList(List<ArrangeInfo> myArrangeList) {
		this.myArrangeList = myArrangeList;
	}

	public View getArrangeActionLayout() {
		return arrangeActionLayout;
	}

	public TextView getArrangeLayout() {
		return arrangeLayout;
	}

	public View getArrangeBtnLayout() {
		return arrangeBtnLayout;
	}

	public Menu getMenu() {
		return menu;
	}
	
}
